package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.youzan.spiderman.utils.StringUtils;

import javax.inject.Inject;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.AdvertisementsRes;
import sz.szsmk.citizencard.model.network.dzbean.res.GetUserInfoRes;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.model.network.dzbean.res.User;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.PersonInfoEditMvpView;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.PersonInfoEditPresenter;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Created by chensf on 17/10/4.
 */

public class PersonInfoEditActivity extends AppBaseActivity implements PersonInfoEditMvpView {

    @Inject
    PersonInfoEditPresenter mPersonInfoEditPresenter;

    EditText et_qr_nike_name;
    CheckBox et_qr_gender_male;
    CheckBox et_qr_gender_female;
    EditText et_qr_email;
    EditText et_qr_degree;
    EditText et_qr_star_sign;
    EditText et_qr_person_signature;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.qr_activity_edit_person_info);

        et_qr_nike_name = findViewById(R.id.et_qr_nike_name);
        et_qr_gender_male = findViewById(R.id.et_qr_gender_male);
        et_qr_gender_female = findViewById(R.id.et_qr_gender_female);
        et_qr_email = findViewById(R.id.et_qr_email);
        et_qr_degree= findViewById(R.id.et_qr_degree);
        et_qr_star_sign = findViewById(R.id.et_qr_star_sign);
        et_qr_person_signature = findViewById(R.id.et_qr_person_signature);

        mPersonInfoEditPresenter.getPersonInfo();

        et_qr_gender_male.setOnClickListener(this::onClickView);
        et_qr_gender_female.setOnClickListener(this::onClickView);

        findViewById(R.id.btn_qr_save).setOnClickListener(this::onClickView);

    }


    public void onClickView(View v){
        if (R.id.et_qr_gender_male == v.getId()){
            et_qr_gender_male.setChecked(true);
            et_qr_gender_female.setChecked(false);
        }else if (R.id.et_qr_gender_female == v.getId()){
            et_qr_gender_male.setChecked(false);
            et_qr_gender_female.setChecked(true);
        }else if(R.id.btn_qr_save == v.getId()){
            String nikeName = et_qr_nike_name.getEditableText().toString();
            if (StringUtils.isEmpty(nikeName)){
                PubUtils.popTipOrWarn(PersonInfoEditActivity.this,getString(R.string.qr_nike_name_empty));
                return;
            }
            String gender = "";
            if (et_qr_gender_male.isChecked()){
                gender = Constant.GENDER_MALE;
            }else if (et_qr_gender_female.isChecked()){
                gender = Constant.GENDER_FEMALE;
            }

            String email = et_qr_email.getEditableText().toString();
            String degree = et_qr_degree.getEditableText().toString();
            String starSign = et_qr_star_sign.getEditableText().toString();
            String personSignature = et_qr_person_signature.getEditableText().toString();

            User user = new User();
            user.setNickName(nikeName);
            user.setGender(gender);
            user.setEmail(email);
            user.setDegree(degree);
            user.setStarSign(starSign);
            user.setPersonalSignature(personSignature);

            mPersonInfoEditPresenter.updatePersonInfo(user);
        }
    }


    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{mPersonInfoEditPresenter};
    }

    @Override
    protected CharSequence getTopTitle() {
        return getString(R.string.qr_title_person_info);
    }

    @Override
    public void onGainPersonInfo(GetUserInfoRes user) {
        if (null != user){
            et_qr_nike_name.setText(user.getData().getNickName());
            if (Constant.GENDER_MALE.equals(user.getData().getGender())){
                et_qr_gender_male.setChecked(true);
                et_qr_gender_female.setChecked(false);
            }else{
                et_qr_gender_male.setChecked(false);
                et_qr_gender_female.setChecked(true);
            }
            et_qr_email.setText(user.getData().getEmail());
            et_qr_degree.setText(user.getData().getDegree());
            et_qr_star_sign.setText(user.getData().getStarSign());
            et_qr_person_signature.setText(user.getData().getPersonalSignature());
        }
    }

    @Override
    public void getAdvertisementsResult(AdvertisementsRes advertisementsRes) {

    }

    @Override
    public void onUpdatePersonInfoSuccess() {
        PubUtils.popTipOrWarn(PersonInfoEditActivity.this,getString(R.string.qr_save_success));
        eventBus.post(new MessageEvent(MessageEvent.CODE_UPDATE_USERINFO_SUCCESS));
        finish();
    }
}
